package app.framework.freemarker.util;

import app.framework.freemarker.model.CurrentLangMethodModel;
import app.framework.freemarker.model.I18nJsMethodModel;
import app.framework.freemarker.model.PagePlaceholderMethodModel;
import app.framework.freemarker.model.AppCssMethodModel;
import app.framework.freemarker.model.AppJsMethodModel;
import app.framework.freemarker.model.AppLangMethodModel;
import app.framework.freemarker.model.I18nHtmlMethodModel;
import app.framework.freemarker.model.LibCssMethodModel;
import app.framework.freemarker.model.LibJsMethodModel;
import app.framework.freemarker.model.LibLangMethodModel;
import freemarker.template.Configuration;
import freemarker.template.Template;
import jasmine.framework.common.util.CheckUtil;
import jasmine.framework.common.util.ErrorUtil;
import liquibase.util.StringUtil;

import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 渲染 HTML 页面的工具类。
 * </p>
 *
 * @author mh.z
 */
public class RenderHtmlUtil {
    private static final Map<String, Object> DATA_MODEL;
    private static final String SYMBOL_BACKSLASH = "/";

    static {
        DATA_MODEL = new HashMap<>();
        initUtil(RenderUtilConfig.createDefault());
    }

    public static void initUtil(RenderUtilConfig config) {
        CheckUtil.notNull(config, "config null");
        String contextPath = CheckUtil.notNull(config.getContextPath(),
                "config.contextPath null");
        Map<String, String> mappingUrlMap = CheckUtil.notNull(config.getMappingUrls(),
                "config.mappingUrls null");
        Map<String, Object> pagePlaceholderMap = CheckUtil.notNull(config.getPagePlaceholders(),
                "config.pagePlaceholders null");

        DATA_MODEL.put("appCss", new AppCssMethodModel(mappingUrlMap));
        DATA_MODEL.put("appJs", new AppJsMethodModel(mappingUrlMap));
        DATA_MODEL.put("appLang", new AppLangMethodModel(mappingUrlMap));
        DATA_MODEL.put("libCss", new LibCssMethodModel(mappingUrlMap));
        DATA_MODEL.put("libJs", new LibJsMethodModel(mappingUrlMap));
        DATA_MODEL.put("libLang", new LibLangMethodModel(mappingUrlMap));
        DATA_MODEL.put("i18nJs", new I18nJsMethodModel());
        DATA_MODEL.put("t", new I18nHtmlMethodModel());
        DATA_MODEL.put("pagePlaceholder", new PagePlaceholderMethodModel(pagePlaceholderMap));
        DATA_MODEL.put("currentLang", new CurrentLangMethodModel());

        // 获取上下文路径
        if (StringUtil.isEmpty(contextPath)) {
            contextPath = SYMBOL_BACKSLASH;
        } else if (!contextPath.endsWith(SYMBOL_BACKSLASH)) {
            contextPath = contextPath + SYMBOL_BACKSLASH;
        }

        DATA_MODEL.put("basePath", contextPath);
    }

    /**
     * 渲染内容
     *
     * @param cfg
     * @param writer
     * @param name
     */
    public static void renderContent(Configuration cfg, Writer writer, String name) {
        CheckUtil.notNull(cfg, "cfg null");
        CheckUtil.notNull(writer, "writer null");
        CheckUtil.notNull(name, "name null");

        try {
            Template template = cfg.getTemplate(name);
            template.process(DATA_MODEL, writer);
        } catch (Exception e) {
            throw ErrorUtil.sneakyError(e);
        }
    }

}
